package com.xone.xapp;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class UserCenterActivity extends BasicActivity {
	@Override
	public int getContentView() {
		return R.layout.user_center;
	}
	@Override
	public String getHeaderTitle() {
		return getTitleByLevel("用户中心");
	}
	@Override
	public void leftButton(Button leftButton) {
		if (UserInfo.login) {
			leftButton.setText("退出");
			leftButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					new AlertDialog.Builder(getContext())
					.setTitle("消息确认")
					.setMessage("确认退出登录吗？")
					.setPositiveButton(android.R.string.ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									showProgress();
									new LogoutDataTask(getContext()) {
										@Override
										public void onFinish(JSONObject resutl) {
											hideProgress();
										}
									}.execute();
								}
							})
					.setNegativeButton(android.R.string.cancel,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									
								}
							}).create().show();
				}
			});
		} else {
			leftButton.setText("登录");
			leftButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent i = new Intent(getContext(), LoginActivity.class);
					startActivity(i);
				}
			});
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//权限控制
		if (UserInfo.alevelUser || UserInfo.blevelUser) {//售卖列表
			findViewById(R.id.to_sale_center).setVisibility(View.VISIBLE);
			if (UserInfo.alevelUser) {
				findViewById(R.id.product_userlist_line_2).setVisibility(View.VISIBLE);
				findViewById(R.id.product_userlist_type_2).setVisibility(View.VISIBLE);
			}
			findViewById(R.id.support_userlist_line_0).setVisibility(View.VISIBLE);
			findViewById(R.id.support_userlist_type_0).setVisibility(View.VISIBLE);
			findViewById(R.id.support_userlist_line_1).setVisibility(View.VISIBLE);
			findViewById(R.id.support_userlist_type_1).setVisibility(View.VISIBLE);
			findViewById(R.id.support_userlist_line_2).setVisibility(View.VISIBLE);
			findViewById(R.id.support_userlist_type_2).setVisibility(View.VISIBLE);
		}
		if (UserInfo.blevelUser || UserInfo.clevelUser) {//求购列表
			findViewById(R.id.to_purchase_center).setVisibility(View.VISIBLE);
			findViewById(R.id.support_userlist_type_4).setVisibility(View.VISIBLE);
			findViewById(R.id.support_userlist_line_4).setVisibility(View.VISIBLE);
		}
		View userInfoType = findViewById(R.id.user_info_details);
		userInfoType.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, UserInfoItemActivity.class);
				startActivity(i);
			}
		});
		View productUserListType0 = findViewById(R.id.product_userlist_type_0);
		productUserListType0.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, ProductListUserActivity.class);
				i.putExtra("saleType", "0");
				startActivity(i);
			}
		});
		View productUserListType1 = findViewById(R.id.product_userlist_type_1);
		productUserListType1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, ProductListUserActivity.class);
				i.putExtra("saleType", "1");
				startActivity(i);
			}
		});
		View productUserListType2 = findViewById(R.id.product_userlist_type_2);
		productUserListType2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, ProductListUserActivity.class);
				i.putExtra("saleType", "2");
				startActivity(i);
			}
		});
		View purchaseUserListType0 = findViewById(R.id.purchase_userlist_type_0);
		purchaseUserListType0.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, PurchaseListUserActivity.class);
				startActivity(i);
			}
		});
		View supportUserListType0 = findViewById(R.id.support_userlist_type_0);
		supportUserListType0.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, DeliveryListActivity.class);
				startActivity(i);
			}
		});
		View supportUserListType1 = findViewById(R.id.support_userlist_type_1);
		supportUserListType1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, BannerListUserActivity.class);
				startActivity(i);
			}
		});
		View supportUserListType2 = findViewById(R.id.support_userlist_type_2);
		supportUserListType2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, OverheadListUserActivity.class);
				startActivity(i);
			}
		});
		View supportUserListType3 = findViewById(R.id.support_userlist_type_3);
		supportUserListType3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, SubscribeListUserActivity.class);
				startActivity(i);
			}
		});
		View supportUserListType4 = findViewById(R.id.support_userlist_type_4);
		supportUserListType4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(UserCenterActivity.this, ProductGroupListUserActivity.class);
				startActivity(i);
			}
		});
		View supportUserListType5 = findViewById(R.id.support_userlist_type_5);
		supportUserListType5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//清除缓存
				showProgress();
				new ClearResourceTask(getContext()).execute();
			}
		});
		
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	    this.finish();
	}
	
	protected class ClearResourceTask extends AsyncTask<String, Integer, Boolean> {
		protected FileCache cache;
		protected Context context;
		public ClearResourceTask(Context context) {
			this.context = context;
			cache = new FileCache(context);
		}
		@Override
		protected Boolean doInBackground(String... params) {
			cache.clear();
			return true;
		}
		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			hideProgress();
			Toast.makeText(context, "清除完毕", Toast.LENGTH_SHORT).show();
		}
	}
	
}
